#include "IOServer.h"

void AnalogOutput::getValue()
{
    char buf[64];
    snprintf(buf, 64, "data:%02d", value);
    webGuiServer->send(200, "text/plain", buf);
}

void AnalogOutput::setValue()
{
    value = manual ? webGuiServer->arg("plain").toInt() : value;
    webGuiServer->send ( 200, "text/plain", "");
}

void AnalogOutput::setOverride()
{
    manual = !manual;
    webGuiServer->send ( 200, "text/plain", "");
}

void AnalogOutput::getOverride()
{
    webGuiServer->send(200, "text/plain", manual ? "MANUAL" : "AUTO");
}

void AnalogOutput::init()
{
    if (mode != WEB_IO_LOGIC)
    {
        pinMode(pin, OUTPUT);
    }
    webGuiServer->on(getValueUri.c_str(),[&]{getValue();});
    webGuiServer->on(setValueUri.c_str(),[&]{setValue();});
    webGuiServer->on(getOverrideUri.c_str(),[&]{getOverride();});
    webGuiServer->on(setOverrideUri.c_str(),[&]{setOverride();});
}

void AnalogOutput::update()
{
    if (mode != WEB_IO_LOGIC)
    {
        analogWrite(pin, value);
    }
}

void AnalogOutput::createWidget(std::string name, int interval)
{
    loopFunc = "\
        setInterval(" + name + "Loop, " + std::to_string(interval) + ");";
    
    HTML = "\
        <div style=\"border:1px solid black; width:200px; float:left; margin-left: 30px;\">\
            <p id=\"" + name + "_value\">" + name + ": 0</p>\
            <p id=\"" + name + "_control\">AUTO</p>\
            <input id=\"" + name + "_manual\" type=\"button\" value=\"Override\" onclick=\"set_" + name + "_manual()\"/><br>\
            <input id=\"" + name + "_adjust\" type=\"text\" size=\"4\" value=\"0\"/>\
            <input id=\"" + name + "_submit\" type=\"button\" value=\"Submit\" onclick=\"" + name + "_submit()\"/>\
        </div>";
    
    JS = "\
        function " + name + "Loop() {\
            document.getElementById(\"" + name + "_control\").innerHTML = get_" + name + "_manual();\
            var response = get_" + name + "();\
            var value = response.split(\":\")[1].trim();\
            document.getElementById(\"" + name + "_value\").innerHTML =" + "\"" + name + ": \" + value;\
        }\
        function get_" + name + "() {\
            var xmlHttp = new XMLHttpRequest();\
            xmlHttp.open(\"GET\", \"/get_" + name + "\", false );\
            xmlHttp.send( null );\
            return xmlHttp.responseText;\
        }\
        function " + name + "_submit() {\
            var xmlHttp = new XMLHttpRequest();\
            xmlHttp.open(\"POST\", \"/set_" + name + "\", false );\
            xmlHttp.send(document.getElementById(\"" + name + "_adjust\").value);\
            return xmlHttp.responseText;\
        }\
        function set_" + name + "_manual() {\
            var xmlHttp = new XMLHttpRequest();\
            xmlHttp.open(\"GET\", \"/set_override_" + name + "\", false );\
            xmlHttp.send( null );\
            return xmlHttp.responseText;\
        }\
        function get_" + name + "_manual() {\
            var xmlHttp = new XMLHttpRequest();\
            xmlHttp.open(\"GET\", \"/get_override_" + name + "\", false );\
            xmlHttp.send( null );\
            return xmlHttp.responseText;\
        }";
}

AnalogOutput::AnalogOutput(std::string name, int interval) 
{
    ioType = AO;
    setUri(name);
    mode = WEB_IO_LOGIC;
    createWidget(name, interval);
}

AnalogOutput::AnalogOutput(std::string name, uint8_t pin, int interval) 
{
    ioType = AO;
    setUri(name);
    this->pin = pin;
    createWidget(name, interval);
}
